# 用于输出keyword.csv
import csv
from mongo import (
  collection_movie_keywords,
  get_all_data
)
from utils import (
  store_data_to_csv
)
def create_keyword_csv():
  result = []
  # 1.拿到keyword数据
  keyword_data = get_all_data(collection_movie_keywords)
  # 2.根据每个id将keyword数据依次变成a|b|c|d的形式
  for item in keyword_data:
    if "keywords" not in item:
      continue
    keyword_list = item["keywords"]
    # 2.1 提取keywords的name并用"|"连接
    keywords_str = "|".join(keyword["name"] for keyword in keyword_list)
    # 2.2 创建新的数据结构
    new_data = {
        "movieId": item["id"],
        "keywords": keywords_str
    }
    result.append(new_data)
  store_data_to_csv(result, 'keywords.csv')

create_keyword_csv()